package com.citicup.backend.service.FundService;

import com.citicup.backend.entity.FundDaily;
import com.citicup.backend.entity.FundInfo;
import com.citicup.backend.service.FundService.FundComparator.FundComparator;
import org.springframework.data.util.Pair;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class FundSortList {
    static final int NUM_TO_SHOW = 50;

    ArrayList<FundSortInfo> fundSortInfos = new ArrayList<>();

    public void add(FundInfo fundInfo, FundDaily fundDaily) {
        fundSortInfos.add(new FundSortInfo(fundInfo, fundDaily));
    }

    public void sort() {
        ArrayList<Comparator<FundSortInfo>> comparators = FundComparator.getFundComparator();
        for (Comparator<FundSortInfo> comparator : comparators) {
            fundSortInfos.sort(comparator);
            for (int i = 0; i < fundSortInfos.size(); i++) {
                fundSortInfos.get(i).ranks.add(i);
            }
        }
        fundSortInfos.sort(new FundSortInfo.FundSortInfoComparator());
    }

    public List<Pair<FundInfo, FundDaily>> getResult() {
        List<Pair<FundInfo, FundDaily>> pairs = new ArrayList<>();
        for (int i = 0; i < NUM_TO_SHOW; i++) {
            pairs.add(Pair.of(fundSortInfos.get(i).fundInfo, fundSortInfos.get(i).fundDaily));
        }
        return pairs;
    }

    public double getAveMonthRate() {
        double sum = 0;

        for (int i = 0; i < NUM_TO_SHOW; i++) {
            sum += fundSortInfos.get(i).fundDaily.getMonthRate();
        }

        return sum / NUM_TO_SHOW;
    }
}
